package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.model.AlbumItem;
import com.jdy.haoduoaiteacher.util.Tools;
import com.jdy.haoduoaiteacher.util.animation.Rotate3dAnimation;
import com.jdy.haoduoaiteacher.view.ExpandableLayout;
import com.jdy.haoduoaiteacher.view.ExpandableLayout.OnCollapseFinishListener;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//import com.jdy.haoduoaiteacher.activity.MembershipCenterActivity;

/**
 * 
 * 
 * @author wangyanming
 * 
 */
public class StoryNumberListAdapter extends BaseAdapter {

  private final ArrayList<AlbumItem> mList;
  private final Context mContext;

  public int selectedPosition = -1;

  public int selectedRotatePosition = -1;

  // public int curPos;

  public int getSelectedRotatePosition() {
    return selectedRotatePosition;
  }

  public void setSelectedRotatePosition(int selectedRotatePosition) {
    this.selectedRotatePosition = selectedRotatePosition;
    notifyDataSetChanged();
  }

  private int highLightPosition = -1; // 正在播放

  public int getHighLightPosition() {
    return highLightPosition;
  }

  public void setHighLightPosition(int highLightPosition) {
    this.highLightPosition = highLightPosition;
    notifyDataSetChanged();
  }

  private OnMenuClickListener mOnMenuClickListener;

  public OnMenuClickListener getOnMenuClickListener() {
    return mOnMenuClickListener;
  }

  public void setOnMenuClickListener(OnMenuClickListener mOnMenuClickListener) {
    this.mOnMenuClickListener = mOnMenuClickListener;
  }

  public StoryNumberListAdapter(ArrayList<AlbumItem> mList, Context mContext) {
    super();
    this.mList = mList;
    this.mContext = mContext;
  }

  @Override
  public int getCount() {
    if (mList == null) {
      return 0;
    } else {
      return this.mList.size();
    }
  }

  @Override
  public Object getItem(int position) {
    if (mList == null) {
      return null;
    } else {
      return this.mList.get(position);
    }
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  public interface OnMenuClickListener {
    public void OnClickAddToList(int id, String name, String album_name,
                                 String subtitle, String reslen, String order);

    public void OnClickStoryList(int id, String name, String subtitle,
                                 String reslen, String order);

    public void OnClickDownload(int id, String name, String subtitle,
                                String reslen, String order);

    public void OnClickFavorite(int id, String name, String subtitle,
                                String reslen, String order);

    public void OnClickTalk(int id, String name, String subtitle,
                            String reslen, String order);

    // public void OnClickMore()

    public void OnClickMore(int pos, AlbumItem ai, View moreV);
  }

  // public int Rowline = -1;

  @SuppressLint({ "InflateParams", "ResourceAsColor" })
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (this.mList == null || this.mList.size() == 0) {
      return convertView;
    }

    final AlbumItem ai = this.mList.get(position);
    final String album_name = ai.album_name;
    final String name = ai.title;
    final boolean isFav = ai.favorite;
    final String order = ai.sort + "";
    final String reslen = Tools.secToTime(ai.length);
    final String subtitle = "";

    String type = "";
    if (ai.tags != null) {
      type = ai.tags;
    }

    final int id = ai.id;

    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(this.mContext).inflate(
          R.layout.story_number_listitem, null, false);
      holder.storynumber = (TextView) convertView
          .findViewById(R.id.storynumber);
      holder.storytittle = (TextView) convertView
          .findViewById(R.id.storytittle);
      holder.singleNewImageView = (ImageView) convertView
          .findViewById(R.id.single_new_imageview);
      holder.addlist = (LinearLayout) convertView.findViewById(R.id.addlist);
      holder.more_layout = (LinearLayout) convertView
          .findViewById(R.id.more_layout);
      final ImageView love_or_not_img = (ImageView) convertView
          .findViewById(R.id.love_or_not_img);
      holder.loveStateImg = love_or_not_img;

      // holder.loveLayout.setOnClickListener(new View.OnClickListener() {
      //
      // @Override
      // public void onClick(View v) {
      // mOnMenuClickListener.OnClickLoveBtn(position, (AlbumItem) v.getTag(),
      // love_or_not_img);
      // }
      // });

      holder.moreLayout = (ExpandableLayout) convertView
          .findViewById(R.id.list_item_more_layout);

      holder.tv_seqno = (TextView) convertView.findViewById(R.id.tv_seqno);

      holder.tv_addtolist = (TextView) convertView
          .findViewById(R.id.tv_addtolist);
      holder.tv_storylist = (TextView) convertView
          .findViewById(R.id.tv_storylist);
      holder.tv_download = (TextView) convertView
          .findViewById(R.id.tv_download);
      holder.tv_favorite = (TextView) convertView
          .findViewById(R.id.tv_favorite);
      holder.apply_btn = (Button) convertView.findViewById(R.id.apply_btn);
      holder.tv_talk = (TextView) convertView.findViewById(R.id.tv_talk);

      holder.img_diamond = (ImageView) convertView
          .findViewById(R.id.img_diamond);
      holder.img_playing_icon = (ImageView) convertView
          .findViewById(R.id.img_playing_icon);

      holder.frame_view = (FrameLayout) convertView
          .findViewById(R.id.frame_view);

      holder.rootview = convertView.findViewById(R.id.rootview);

      // holder.linearLayout1 = convertView.findViewById(R.id.linearLayout1);
      holder.vip_tip_layout = convertView.findViewById(R.id.vip_tip_layout);

      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    holder.more_layout.setTag(ai);

    final View moreV = holder.more_layout;

    final ImageView love_or_not_img = (ImageView) convertView
        .findViewById(R.id.love_or_not_img);
    holder.more_layout.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
        mOnMenuClickListener.OnClickMore(position, (AlbumItem) v.getTag(),
            moreV);

        // ai.showMore = true;
        //
        // moreV.setVisibility(View.GONE);
        // Rowline = position;
      }
    });

    // if (ai.showMore) {
    // moreV.setVisibility(View.GONE);
    // } else {
    // moreV.setVisibility(View.VISIBLE);
    // }

    // if (curPos == -1 && position == Rowline && Rowline > -1) {
    // moreV.setVisibility(View.VISIBLE);
    //
    // curPos = -2;
    // Rowline = -1;
    // }

    // if (isFav) {
    // holder.loveStateImg.setBackgroundResource(R.drawable.love_already);
    // } else {
    // holder.loveStateImg.setBackgroundResource(R.drawable.not_love_yet);
    // }

    final ExpandableLayout expandView = holder.moreLayout;

    if (this.mList != null) {
      if (holder.storynumber != null) {

        if (type.equals("new")) {
          holder.singleNewImageView.setVisibility(View.VISIBLE);
        } else {
          holder.singleNewImageView.setVisibility(View.GONE);
        }

        if ((position + 1) < 10) {
          holder.tv_seqno.setText("0" + (position + 1));
        } else {
          holder.tv_seqno.setText("" + (position + 1));
        }

        if (highLightPosition == position) {
          holder.storynumber.setTextColor(Color.parseColor("#f6b559"));
          holder.img_playing_icon.setVisibility(View.VISIBLE);
          holder.frame_view.setVisibility(View.INVISIBLE);
        } else {
          holder.img_playing_icon.setVisibility(View.GONE);
          holder.frame_view.setVisibility(View.VISIBLE);
          holder.storynumber.setTextColor(mContext.getResources().getColor(
              R.color.black_color));
        }

        final View vip_tip_layout = holder.vip_tip_layout;

        holder.storynumber.setText(album_name + " 第" + order + "集" + " ("
            + reslen + ")");

        if (Tools.isEmpty(subtitle)) {
          holder.storytittle.setText(name);
        } else {
          holder.storytittle.setText(subtitle);
        }

        if (selectedRotatePosition == position
            && vip_tip_layout.getVisibility() != View.VISIBLE) {
          Rotate3dAnimation anim = Tools.get3DRotationAnim(
              vip_tip_layout.getWidth(), vip_tip_layout.getHeight(), -90, 0,
              false);
          anim.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }
          });

          vip_tip_layout.startAnimation(anim);
          vip_tip_layout.setVisibility(View.VISIBLE);
        } else {
          vip_tip_layout.clearAnimation();
          if (vip_tip_layout.getVisibility() == View.VISIBLE) {
            vip_tip_layout.setVisibility(View.GONE);
          }
        }

        // holder.linearLayout1.setOnClickListener(new OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // }
        // });

        holder.apply_btn.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
//            Intent toMembership = new Intent(mContext,
//                MembershipCenterActivity.class);
//            toMembership.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
//                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            mContext.startActivity(toMembership);
          }
        });

        holder.addlist.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            int visable = expandView.getVisibility();
            if (visable == View.VISIBLE) {
              expandView.setLayoutHeight(150);
              expandView
                  .setOnCollapseFinishListener(new OnCollapseFinishListener() {

                    @Override
                    public void onCollapseFinish() {
                      selectedPosition = -1;
                    }

                  });
              expandView.collapse();

            } else {
              expandView.setLayoutHeight(150);
              expandView.expand();
              selectedPosition = position;

              selectedRotatePosition = -1;
            }
            notifyDataSetChanged();
          }
        });

        changeClickBgColor(holder.tv_addtolist);
        holder.tv_addtolist.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (mOnMenuClickListener != null) {
              mOnMenuClickListener.OnClickAddToList(id, name, album_name,
                  subtitle, reslen, order);
            }
          }
        });

        changeClickBgColor(holder.tv_storylist);
        holder.tv_storylist.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (mOnMenuClickListener != null) {
              mOnMenuClickListener.OnClickStoryList(id, name, subtitle, reslen,
                  order);
            }
          }
        });

        changeClickBgColor(holder.tv_download);
        holder.tv_download.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (mOnMenuClickListener != null) {
              mOnMenuClickListener.OnClickDownload(id, name, subtitle, reslen,
                  order);
            }
          }
        });

        changeClickBgColor(holder.tv_favorite);
        holder.tv_favorite.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (mOnMenuClickListener != null) {
              mOnMenuClickListener.OnClickFavorite(id, name, subtitle, reslen,
                  order);
            }
          }
        });

        changeClickBgColor(holder.tv_talk);
        holder.tv_talk.setOnClickListener(new OnClickListener() {

          @Override
          public void onClick(View v) {
            if (mOnMenuClickListener != null) {
              mOnMenuClickListener.OnClickTalk(id, name, subtitle, reslen,
                  order);
            }
          }
        });

        if (selectedPosition == position) {
          holder.moreLayout.setVisibility(View.VISIBLE);
        } else {
          holder.moreLayout.setVisibility(View.GONE);
        }
      }
    }
    return convertView;
  }

  // public String getTime(int reslen) {
  // int min = reslen / 60;
  // int s = reslen % 60;
  // String ss;
  // if (s < 10)
  // ss = "0" + s;
  // else
  // ss = String.valueOf(s);
  // return min + ":" + ss;
  // }

  private void changeClickBgColor(final TextView tvIndex) {
    tvIndex.setOnTouchListener(new OnTouchListener() {
      @SuppressLint("ClickableViewAccessibility")
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          tvIndex.setTextColor(mContext.getResources().getColor(
              R.color.orange_color));
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          tvIndex.setTextColor(mContext.getResources().getColor(
              R.color.white_color));
        }
        return false;
      }
    });
  }

  private class ViewHolder {
    TextView storynumber;
    TextView storytittle;
    @SuppressWarnings("unused")
    ImageView singleNewImageView, img_playing_icon, img_diamond;
    LinearLayout addlist;
    LinearLayout more_layout;
    ImageView loveStateImg;
    FrameLayout frame_view;

    ExpandableLayout moreLayout;

    TextView tv_addtolist;
    TextView tv_storylist;
    TextView tv_download;
    TextView tv_favorite;
    TextView tv_talk;

    View vip_tip_layout, rootview;
    Button apply_btn;

    TextView tv_seqno;
  }

}
